package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import action.ActionChangerLangue;
import action.ActionConnecter;
import action.ActionQuitter;

public class FenetreConnexion extends JFrame implements KeyListener, GenericForm{

	private JMenuBar menuConnexion;
	private JMenu menuLangues;
	private JMenuItem itemMAnglais;
	private JMenuItem itemMFrancais;

	private JPanel mainPanel;
	private JPanel panelLogin;
	private JPanel panelMDP;
	private JPanel pnlButton;

	private JLabel lblLogin;
	private JLabel lblMdp;
	private JLabel lblTitle;
	private JLabel lblErrorLogin;
	private JLabel lblErrorMdp;
	private JLabel lblErrorConnection;

	private JTextField txtFLogin;
	private JPasswordField txtFMdp;

	private JButton btnConnect;
	private JButton btnQuit;
	private JButton btnRegistration;
	
	private Locale locale;
	private ResourceBundle bundle;
	private FenetreConnexion currentForm;

	public FenetreConnexion(){
		super();
		currentForm = this;
		locale = Locale.getDefault();
		bundle = ResourceBundle.getBundle("ressources", locale);
		build();
		this.setLangue(bundle);
	}

	public void build(){
		setSize(350,180);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setJMenuBar(buildMenuBar());
		setContentPane(buildContentPane());

		this.setVisible(true);

	}

	public JMenuBar buildMenuBar(){
		menuConnexion = new JMenuBar();
		menuLangues = new JMenu("");
		itemMAnglais = new JMenuItem(new ActionChangerLangue(this,bundle.getString("itemMAnglais")));
		itemMFrancais = new JMenuItem(new ActionChangerLangue(this,this.bundle.getString("itemMFrancais")));
		menuLangues.add(itemMAnglais);
		menuLangues.add(itemMFrancais);
		menuConnexion.add(menuLangues);
		return menuConnexion;	
	}

	public JPanel buildContentPane(){
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());

		panelLogin = new JPanel();
		panelLogin.setLayout(new GridBagLayout());
		panelLogin.setBorder(BorderFactory.createTitledBorder(""));

		pnlButton = new JPanel();
		pnlButton.setLayout(new FlowLayout());

		lblLogin = new JLabel("");
		lblMdp = new JLabel("");
		lblErrorLogin = new JLabel("");
		lblErrorMdp = new JLabel("");
		lblErrorConnection = new JLabel("");
		lblErrorConnection.setForeground(new Color(255,0,0));
		lblErrorLogin.setForeground(new Color(255,0,0));
		lblErrorMdp.setForeground(new Color(255,0,0));
		txtFLogin = new JTextField("");
		txtFMdp = new JPasswordField("");
		txtFLogin.setPreferredSize(new Dimension(120, 30));
		txtFMdp.setPreferredSize(new Dimension(120, 30));
		btnConnect = new JButton(new ActionConnecter(this));
		btnQuit = new JButton(new ActionQuitter(""));
		btnRegistration = new JButton();
		btnRegistrationListener listener = new btnRegistrationListener();
		btnRegistration.addActionListener(listener);
		txtFMdp.addKeyListener(this);
		txtFLogin.addKeyListener(this);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_START;
		panelLogin.add(lblLogin, gbc);
		gbc.gridx = 0;
		gbc.gridy = 1;
		panelLogin.add(lblMdp,gbc);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_END;
		panelLogin.add(txtFLogin, gbc);
		gbc.gridx = 1;
		gbc.gridy = 1;
		panelLogin.add(txtFMdp, gbc);
		gbc.gridx = 2;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.PAGE_START;
		panelLogin.add(lblErrorLogin, gbc);
		gbc.gridx = 2;
		gbc.gridy = 1;
		panelLogin.add(lblErrorMdp, gbc);

		pnlButton.add(btnConnect);
		pnlButton.add(btnQuit);
		pnlButton.add(btnRegistration);

		mainPanel.add(panelLogin, BorderLayout.CENTER);
		mainPanel.add(pnlButton, BorderLayout.SOUTH);

		return mainPanel;
	}

	public JTextField getLogin(){
		return this.txtFLogin;
	}

	public JTextField getMdp(){
		return this.txtFMdp;
	}

	public void setLangue(ResourceBundle langue){
		this.btnConnect.setText(langue.getString("BtnConnect"));
		this.btnQuit.setText(langue.getString("BtnQuit"));
		this.btnRegistration.setText(langue.getString("btnRegistration"));
		this.lblLogin.setText(langue.getString("lblLogin"));
		this.lblMdp.setText(langue.getString("lblMdp"));
		this.setTitle(langue.getString("Title"));
		this.menuLangues.setText(langue.getString("menuLangues"));
		this.itemMAnglais.setText(langue.getString("itemMAnglais"));
		this.itemMFrancais.setText(langue.getString("itemMFrancais"));
		this.panelLogin.setBorder(BorderFactory.createTitledBorder(langue.getString("borderPanelLogin")));
	}

	public void setErrorConnection(String message){
		this.lblErrorConnection.setText(message);
	}

	public void setErrorLogin(String message){
		this.lblErrorLogin.setText(message); 
	}

	public ResourceBundle getBundle(){
		return this.bundle;
	}

	public Locale getLocale(){
		return this.locale;
	}

	public void setBundle(ResourceBundle bundle){
		this.bundle = bundle;
	}

	public void setErrorMdp(String message){
		this.lblErrorMdp.setText(message); 
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		int KeyCode = e.getKeyCode();
		if (KeyCode == KeyEvent.VK_ENTER){
			btnConnect.doClick();
		}
		else if (KeyCode == KeyEvent.VK_ESCAPE){
			btnQuit.doClick();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public class btnRegistrationListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			currentForm.dispose();
			RegistrationForm registrationForm = new RegistrationForm();	
		}
		
	}

}
